﻿using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace CutShuFa.AutoAction
{
    public class AutoActionData
    {
        public AutoActionData(string path)
        {
            Mat src_mat = Cv2.ImRead(path);
            Mat tmp_mat = src_mat.Clone();
            Mat gray_mat = new Mat();
            matDic = new Dictionary<string, Mat>();
            matDic.Add("src_mat", src_mat);
            matDic.Add("tmp_mat", tmp_mat);
            matDic.Add("gray_mat", gray_mat);
            Cv2.CvtColor(src_mat, gray_mat, ColorConversionCodes.BGR2GRAY);
        }
        public Dictionary<string, Mat> matDic;
        public OpenCvSharp.Point[][] contours = null;

        public void RemoveContoursByArea(int area)
        {
            if (contours == null)
                return; 
            contours = contours.Where(x => Cv2.ContourArea(x) > area).ToArray();
        }

        public Bitmap SrcBitmap
        {
            get
            {
                return BitmapConverter.ToBitmap(matDic["src_mat"]);
            }
        }

        public Bitmap TmpBitmap
        {
            get
            {
                return BitmapConverter.ToBitmap(matDic["tmp_mat"]);
            }
        }
        
        public Bitmap GrayBitmap
        {
            get
            {
                return BitmapConverter.ToBitmap(matDic["gray_mat"]);
            }
        }

        public void Save(string path)
        {
            Cv2.ImWrite(path, matDic["src_mat"]);
        }
    }
}
